package com.smt.take.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AbsListView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.smt.take.R
import com.smt.take.model.beans.GoodsBean
import com.smt.take.model.beans.GoodsDetailsBean
import com.smt.take.presenter.GoodsPresenter
import com.smt.take.ui.activity.BusinessActivity
import com.smt.take.ui.adapter.GoodsAdapter
import com.smt.take.ui.adapter.GoodsTypeRvAdapter
import kotlinx.android.synthetic.main.fragment_goods.*
import se.emilsjolander.stickylistheaders.StickyListHeadersListView

class GoodsFragment : Fragment() {

    lateinit var presenter: GoodsPresenter
    lateinit var goodsAdapter: GoodsAdapter
    lateinit var goodsTypeAdapter: GoodsTypeRvAdapter
    lateinit var rvType: RecyclerView
    private lateinit var rvGoods: StickyListHeadersListView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val view = LayoutInflater.from(context).inflate(R.layout.fragment_goods, container, false)
        presenter = GoodsPresenter(this)
        rvGoods = view.findViewById(R.id.slhlv)
        goodsAdapter = GoodsAdapter(context, this)
        rvGoods.adapter = goodsAdapter
        rvType = view.findViewById(R.id.rv_goods_type)
        goodsTypeAdapter = GoodsTypeRvAdapter(context, this)
        rvType.adapter = goodsTypeAdapter
        rvType.layoutManager = LinearLayoutManager(context)
        return view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        presenter.getGoodsInfo((activity as BusinessActivity).seller.id.toString())
    }

    fun onGoodsTypeSuccess(data: GoodsBean?, goodsList: MutableList<GoodsDetailsBean>) {

        data?.list?.let {
            goodsTypeAdapter.setData(it)
        }
        goodsList.let {
            goodsAdapter.setData(it)
        }

        slhlv.setOnScrollListener(object : AbsListView.OnScrollListener {
            override fun onScrollStateChanged(view: AbsListView?, scrollState: Int) {

            }

            override fun onScroll(
                view: AbsListView?,
                firstVisibleItem: Int,
                visibleItemCount: Int,
                totalItemCount: Int
            ) {
                // 先找出旧的类别position
                val oldPosition = goodsTypeAdapter.selectPosition
                val newTypeId = presenter.goodsList[firstVisibleItem].typeId
                // 通过新的typeId找到对应的position
                val newPosition = presenter.getTypePositionByTypeId(newTypeId)
                // 当newPosition与旧的不同时，证明需要切换类别了
                if (newPosition != oldPosition) {
                    goodsTypeAdapter.selectPosition = newPosition
                    goodsTypeAdapter.notifyDataSetChanged()
                }
            }

        })
    }
}